﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace BuildFileIndex
{
    class Program
    {
        static void Main(string[] args)
        {
            string basePath = System.Environment.CurrentDirectory.ToString() + @"\..\..\..\TouchPPT\";
            string imagePath = basePath + @"Images\";
            string catalogueIndex = basePath + @"catalogue.xml";
            string imageIndex = basePath + @"images.xml";
            string mediaPath = basePath + @"Media\";
            string mediaIndex = basePath + @"media.xml";

            List<string> catalogue = new List<string>();
            List<string> images = GetFileNames_TwoLayers(imagePath, ref catalogue);
            List<string> media = GetFileNames_OneLayer(mediaPath, ref catalogue);

            System.Console.WriteLine(System.Environment.CurrentDirectory.ToString());
            System.Console.WriteLine(basePath);

            WriteListToFile(catalogueIndex, catalogue);
            WriteListToFile(imageIndex, images);
            WriteListToFile(mediaIndex, media);

            catalogue = ReadListFromFile(catalogueIndex);
            images = ReadListFromFile(imageIndex);
            media = ReadListFromFile(mediaIndex);

            Console.WriteLine("//////////////////////////////////////////////////////////////////////////");

            foreach (string s in catalogue)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("//////////////////////////////////////////////////////////////////////////");

            foreach (string s in images)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("//////////////////////////////////////////////////////////////////////////");

            foreach (string s in media)
            {
                Console.WriteLine(s);
            }
        }

        public static List<string> GetFileNames_TwoLayers(string path, ref List<string> catalogue)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] dirs = dir.GetDirectories();

            List<string> l = new List<string>();
            foreach (DirectoryInfo subdir in dirs)
            {
                catalogue.Add(subdir.Name);
                FileInfo[] files = subdir.GetFiles();
                StringBuilder sb = new StringBuilder();
                foreach (FileInfo f in files)
                {
                    sb.AppendLine(dir.Name + "/" + subdir.Name + "/" + f.Name);
                }
                l.Add(sb.ToString());
            }
            return l;
        }

        public static List<string> GetFileNames_OneLayer(string path, ref List<string> catalogue)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            List<string> l = new List<string>();
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo f in files)
            {
                l.Add(dir.Name + "/" + f.Name);
                catalogue.Add(f.Name);
            }
            return l;
        }

        public static List<string> ParseStringIntoList(string s)
        {
            StringReader sr = new StringReader(s);
            List<string> list = new List<string>();
            string str;
            do
            {
                str = sr.ReadLine();
                if (str != null)
                {
                    list.Add(str);
                }
            } while (str != null);
            return list;
        }

        public static void WriteListToFile(string path, List<string> list)
        {
            // Create a new XmlSerializer instance with the type of the test class
            XmlSerializer SerializerObj = new XmlSerializer(typeof(List<string>));

            // Create a new file stream to write the serialized object to a file
            TextWriter WriteFileStream = new StreamWriter(path);
            SerializerObj.Serialize(WriteFileStream, list);

            // Cleanup
            WriteFileStream.Close();
        }

        public static List<string> ReadListFromFile(string path)
        {
            // Create a new XmlSerializer instance with the type of the test class
            XmlSerializer SerializerObj = new XmlSerializer(typeof(List<string>));

            // Create a new file stream for reading the XML file
            FileStream ReadFileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            // Load the object saved above by using the Deserialize function
            List<string> list = (List<string>)SerializerObj.Deserialize(ReadFileStream);

            // Cleanup
            ReadFileStream.Close();

            return list;
        }

    }
}
